package com.gsm.phimhanhdong;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.analytics.tracking.android.EasyTracker;
import com.gsm.phimhanhdong.ContentNetworkInterface.GetAccesstokenSuccessEvent;
import com.gsm.phimhanhdong.ContentNetworkInterface.GetRequesttokenSuccessEvent;
import com.gsm.phimhanhdong.ContentNetworkInterface.OnReload;
import com.gsm.phimhanhdong.ContentNetworkInterface.PageBannerSuccessEvent;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.viewpagerindicator.CirclePageIndicator;

import de.greenrobot.event.EventBus;

public class BannerFragement extends Fragment {
	ViewPager viewPager;
	ArrayList<BannerItem> bannerItems;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	CirclePageIndicator indicator;
	MAdapter adapter;
	EventBus eventBus;

	public BannerFragement() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_banner_newsfeed, null);
		viewPager = (ViewPager) view.findViewById(R.id.banner_pager);
		indicator = (CirclePageIndicator) view
				.findViewById(R.id.page_indicator);
		return view;
	}

	public void onDestroyView() {
		super.onDestroyView();
		EventBus.getDefault().unregister(this);
	};

	BackgroundTasks bgTasks;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initImageLoader();
		EventBus.getDefault().register(this);
		eventBus = EventBus.getDefault();
		bannerItems = new ArrayList<BannerItem>();
		bgTasks = new BackgroundTasks(getActivity());
		bgTasks.getRequestToken();

	}
	
	public void onEventMainThread(OnReload e){
		bgTasks.getRequestToken();
	}

	public void onEventMainThread(GetRequesttokenSuccessEvent e) {
		String request_token = e.request_token;
		bgTasks.getAccessToken(request_token);
	}

	public void onEventMainThread(GetAccesstokenSuccessEvent e) {

		String access_token = e.access_token;
		if (access_token != null) {
			bgTasks.getBannerByPage(access_token, Constants.ANDROID_STORE,
					Constants.NEWSFEED_BANNER_PAGE);
		}
	}

	public void onEventMainThread(PageBannerSuccessEvent e) {
		bannerItems= new ArrayList<BannerItem>();
		for (BannerItem bannerItem : e.getBannerList()) {
			bannerItems.add(bannerItem);
		}
		Log.e("", "banner items " + bannerItems.size());
		if(adapter==null){
			adapter = new MAdapter(getActivity(), bannerItems);
			viewPager.setAdapter(adapter);
		}else{
			adapter.notifyDataSetChanged();
		}
		indicator.setViewPager(viewPager);
		indicator.setFillColor(Color.CYAN);
		indicator.setStrokeColor(Color.GRAY);
	}

	private void initImageLoader() {
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.ic_launcher).cacheInMemory()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED).cacheOnDisc()
				.build();
		imageLoader.clearDiscCache();
		imageLoader.clearMemoryCache();
	}

	public class MAdapter extends PagerAdapter {
		Context context;
		ArrayList<BannerItem> bannerItems;

		public MAdapter(Context context, ArrayList<BannerItem> bannerItems) {
			this.context = context;
			this.bannerItems = bannerItems;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return bannerItems.size();
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			super.destroyItem(container, position, object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// TODO Auto-generated method stub
			return (view == object);
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			// TODO Auto-generated method stub
			View view = LayoutInflater.from(context).inflate(
					R.layout.slide_banner_item, null);
			ImageView img = (ImageView) view.findViewById(R.id.banner_image);
			Log.e("", "url " + bannerItems.get(position).getUrl());
			imageLoader.displayImage(bannerItems.get(position).getImages(),
					img, options);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent1 = new Intent();
					intent1.setAction(Intent.ACTION_VIEW);
					intent1.addCategory(Intent.CATEGORY_BROWSABLE);
					intent1.setData(Uri.parse(bannerItems.get(position)
							.getUrl()));
					startActivity(intent1);
				}
			});
			container.addView(view);
			return view;
		}

	}
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(getActivity());
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getTracker().sendView("BannerFragement");
	}

}
